const https = require('https')
const http = require('http')


http.createServer((req, res) => {

  res.writeHead(200,{
    "Content-Type": "application/json;charset=utf-8",
    "access-control-allow-origin": "*"
  })
  console.log(111)
  // 作为中间层，去猫眼拿数据，然后返回给自己
  if(req.url == '/a') {
    httpGet((data) => {
      res.end(data)
    })
  } 

}).listen(5556,() => {
  console.log('5556 server runing')
})


function httpGet (cb) {
  let data = ''
  const url = 'https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json?ct=%E4%B8%8A%E6%B5%B7&ci=10&channelId=4'
  https.get(url, (res) => {
    // 数据以流的形式接收
    res.on('data', (chunk) => {
      data += chunk
    })
    // 接收完出发end事件
    res.on('end', () => {
      console.log(data)
      cb(data)
    })
  })
}